package com.rui.lock;

import java.util.concurrent.atomic.AtomicReference;

public class DemoSpinlock {

    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void mylock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + " ==> mylock");

        //自旋锁
        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void myunlock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + " ==> myunlock");
        atomicReference.compareAndSet(thread, null);
    }
}
